import ImageDownloader from './ImageDownloader'
import LayerManager from './layer-manager'
import DynLayerManager from './dyn-layer-manager'

import $ from 'jquery'
import {initMap} from './map-instance'
import UIManger from './ui-manager'

import {positions, documents} from './globals-en';

//全局飞行状态，是否在飞行
var flyStatus = false;
var map = initMap();
UIManger.manBackground(map);
UIManger.manRain(map);

window._map_ = map;

//初始化
map.on('load', function () {

    //先下载图片
    ImageDownloader.beginDownload(map);

    LayerManager.addLayers(map);

    DynLayerManager.addUploadAndDownloadMarker(map);
    DynLayerManager.addIntoAirMarker(map);
    DynLayerManager.addDynHistogram(map);
    DynLayerManager.addBianDaoMarker(map);
})

map.on('click', function (e) {

    flyStatus = false;
    clearTimeout(myTimeout);
    enableFlyDetect = false;
    posCount = 0;
    map.stop();
});

var flyBut = document.getElementById('fly-button');
var posCount = 0;
var endPos = -1;//这个变量是用于控制飞行停止的，如果不设置就不用处理
var enableFlyDetect = false;
var myTimeout = null;

function toggleMapFlyStatus() {
    flyStatus = !flyStatus;
    if (flyStatus) {
        enableFlyDetect = true;
        map.easeTo(positions[posCount]);
        map.once('moveend', endFun);
        flyBut.setAttribute('src', 'texture/huhuai/icon-fly-stop.png');
    } else {
        map.off('moveend', endFun);
        clearTimeout(myTimeout);
        enableFlyDetect = false;
        posCount = 0;
        map.stop();
        flyBut.setAttribute('src', 'texture/huhuai/icon-fly-start.png');
    }
}

flyBut.onclick = function (ev) {
    toggleMapFlyStatus();
}

function endFun() {

    //如果有posCount的话，就让他去设置
    if (positions[posCount].toCount) {
        minemap.TRW.complex3dSeq.push({count: positions[posCount].toCount})
    }

    if (endPos != -1 && posCount === endPos) {
        toggleMapFlyStatus();
        return;
    }

    if (posCount >= 6) {
        posCount = 0;
    }

    myTimeout = setTimeout(function () {
        posCount++;
        map.easeTo(positions[posCount]);
        map.once('moveend', endFun);
    }, positions[posCount].waiting || 1000);
}

//事件处理

var activeCardSeq = 0;
var numSeq = 1;

$("#next-card").click(function (e) {
    if (flyStatus) {
        toggleMapFlyStatus();
    }
    moveNextFunction(e);
});


function moveNextFunction(e) {
    if (numSeq >= 4) return;

    map.repaint = false;

    var visibleCard = $("#card" + activeCardSeq);
    //先做个检查，如果是用户频繁操作的话，需要清掉所有错误的class
    visibleCard.removeClass('fadeOut fadeInUp');
    visibleCard.addClass('fadeOut');

    visibleCard.unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');

    visibleCard.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
        visibleCard.removeClass('card-active fadeOut fadeInUp');
        map.repaint = true;
    });

    activeCardSeq++;
    activeCardSeq %= 2;
    var noneVisibleCard = $("#card" + activeCardSeq);
    //先做个检查，如果是用户频繁操作的话，需要清掉所有错误的class
    noneVisibleCard.removeClass('fadeInUp fadeOut');
    noneVisibleCard.addClass('card-active fadeInUp');
    noneVisibleCard.unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');

    noneVisibleCard.find('.num-within').attr('src', `texture/control-panel/img-${numSeq + 1}.png`);
    hideNumLabel(numSeq);

    //序号标签移动
    numSeq++;

    //修改文案
    noneVisibleCard.find('.info-title').text(documents['page' + numSeq].title);
    noneVisibleCard.find('.description').text(documents['page' + numSeq].content);

    noneVisibleCard.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
        noneVisibleCard.removeClass('fadeInUp fadeOut');
        var numWithIn = noneVisibleCard.find('.num-within');
        numWithIn.attr('src', `texture/control-panel/img-${numSeq}-selected.png`);
        _makeSceneFly(numSeq - 1);
        map.repaint = true;
    });

}

function movePreFunction(e) {
    if (numSeq <= 1) return;

    map.repaint = false;

    var visibleCard = $("#card" + activeCardSeq);
    //先做个检查，如果是用户频繁操作的话，需要清掉所有错误的class
    visibleCard.removeClass('fadeIn fadeOutDown');
    visibleCard.addClass('card-active fadeIn');

    visibleCard.unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');
    visibleCard.find('.num-within').attr('src', `texture/control-panel/img-${numSeq - 1}.png`);
    //修改文案
    visibleCard.find('.info-title').text(documents['page' + (numSeq - 1)].title);
    visibleCard.find('.description').text(documents['page' + (numSeq - 1)].content);
    visibleCard.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
        visibleCard.addClass('card-active');
        visibleCard.removeClass('fadeIn fadeOutDown');
        map.repaint = true;
    });

    activeCardSeq++;
    activeCardSeq %= 2;
    var noneVisibleCard = $("#card" + activeCardSeq);
    //先做个检查，如果是用户频繁操作的话，需要清掉所有错误的class
    noneVisibleCard.removeClass('fadeIn fadeOutDown');
    noneVisibleCard.addClass('card-active fadeOutDown');
    noneVisibleCard.unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');

    noneVisibleCard.find('.num-within').attr('src', `texture/control-panel/img-${numSeq - 1}.png`);
    hideNumLabel(numSeq - 2);

    //序号标签移动
    numSeq--;

    noneVisibleCard.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
        noneVisibleCard.removeClass('card-active fadeIn fadeOutDown');
        var numWithIn = noneVisibleCard.find('.num-within');
        numWithIn.attr('src', `texture/control-panel/img-${numSeq}-selected.png`);
        _makeSceneFly(numSeq - 1);
        map.repaint = true;
    });

}

//重置界面

$("#reset").click(function (e) {
    activeCardSeq = 0;
    numSeq = 1;

    $('.num2').css('visibility', 'visible');
    $('.num3').css('visibility', 'visible');
    $('.num4').css('visibility', 'visible');

    $("#card0").attr('class', 'info-card card-active animated');
    $("#card0").find('.num-within').attr('src', `texture/control-panel/img-1-selected.png`);
    $("#card1").attr('class', 'info-card animated');

    //重置文案
    $("#card0").find('.info-title').text(documents.page1.title);
    $("#card0").find('.description').text(documents.page1.content);

    $('#reset').css('display', 'none');
    map.easeTo(positions[0]);
});

function _makeSceneFly(seq) {
    if (seq === 1) {
        posCount = 1;
        endPos = 2;//这个变量是用于控制飞行停止的，如果不设置就不用处理
        toggleMapFlyStatus();
    } else if (seq === 2) {
        posCount = 3;
        endPos = 4;
        toggleMapFlyStatus();
    } else if (seq === 3) {
        posCount = 5;
        endPos = 6;
        toggleMapFlyStatus();
    } else {
        map.easeTo(positions[0]);
    }


}


function hideNumLabel(seq) {

    //1是up 2是down
    if (seq === 0) {
        $('.num2').css('visibility', 'visible');
        $('.num3').css('visibility', 'visible');
        $('.num4').css('visibility', 'visible');
        $('.text2').css('visibility', 'visible');
        $('.text3').css('visibility', 'visible');
        $('.text4').css('visibility', 'visible');
        $("#reset").css("display", 'none');
    } else if (seq === 1) {
        $('.num2').css('visibility', 'hidden');
        $('.num3').css('visibility', 'visible');
        $('.num4').css('visibility', 'visible');
        $('.text2').css('visibility', 'hidden');
        $('.text3').css('visibility', 'visible');
        $('.text4').css('visibility', 'visible');
        $("#reset").css("display", 'none');
    } else if (seq === 2) {
        $('.num3').css('visibility', 'hidden');
        $('.num4').css('visibility', 'visible');
        $('.text3').css('visibility', 'hidden');
        $('.text4').css('visibility', 'visible');
        $("#reset").css("display", 'none');
    } else if (seq === 3) {
        $('.num4').css('visibility', 'hidden');
        $('.text4').css('visibility', 'hidden');
        $("#reset").css("display", 'block');
    }

}

//监听滚轮事件

var timeNow = 0;
var timeNowFlag = false;

$("#control-panel").bind('mousewheel', function (event, delta) {
    var dir = event.originalEvent.deltaY > 0 ? 'Up' : 'Down';
    if (!timeNow) {
        timeNow = Date.now();
    }

    if (dir == 'Up') {
        if (!timeNowFlag) {
            timeNowFlag = true;
            if (flyStatus) {
                toggleMapFlyStatus();
            }
            moveNextFunction();
        }

        if (timeNowFlag) {
            if (Date.now() - timeNow < 2000) {
                return;
            } else {
                timeNow = Date.now();
                if (flyStatus) {
                    toggleMapFlyStatus();
                }
                moveNextFunction();
            }
        }

    } else {
        if (!timeNowFlag) {
            timeNowFlag = true;
            if (flyStatus) {
                toggleMapFlyStatus();
            }
            movePreFunction();
        }

        if (timeNowFlag) {
            if (Date.now() - timeNow < 2000) {
                return;
            } else {
                timeNow = Date.now();
                if (flyStatus) {
                    toggleMapFlyStatus();
                }
                movePreFunction();
            }
        }
    }
    return false;
});

